package com.learng.springcloud.ctrl;

import com.google.common.collect.Lists;
import com.learng.springcloud.entity.ResultBean;
import com.learng.springcloud.entity.TestRest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by zhouwei on 2020/7/24.
 */
@RestController
@RequestMapping("/testRest")
public class RestTemplateTestCtrl {

    @Value("${server.port}")
    int serverPort;

    @Autowired(required = false)
    RestTemplate restTemplate;

    @RequestMapping("/getForObjectJSON")
    @ResponseBody
    public ResultBean getForObjectJSON(){
        ResultBean<TestRest> resultBean = new ResultBean<>();
        List<TestRest> testRestList = Lists.newArrayList(
                new TestRest(1,"神雕侠侣","杨过"),
                new TestRest(1,"神雕侠侣","小龙女")
        );
        return resultBean.returnSuccess(testRestList);
    }

    @RequestMapping("/getForObject")
    public ResultBean getForObject(){
        //ResultBean resultBean = restTemplate.getForObject("http://localhost:" + serverPort + "/testRest/getForObjectJSON", ResultBean.class);
        ResultBean resultBean = restTemplate.getForObject("http://order-service/testRest/getForObjectJSON", ResultBean.class);
        return resultBean;
    }


    @RequestMapping("/getForObjectJSONParam")
    public ResultBean getForObjectJSONParam(@RequestParam(value = "name", required = false) String name){
        ResultBean<TestRest> resultBean = new ResultBean<>();
        System.out.println("param name:" + name);
        List<TestRest> testRestList = Lists.newArrayList(
                new TestRest(1,"神雕侠侣","杨过"),
                new TestRest(1,"神雕侠侣","小龙女")
        );
        return resultBean.returnSuccess(testRestList);
    }

    @RequestMapping("/getForObjectParam")
    public ResultBean getForObjectParam(@RequestParam(value = "name", required = false) String name){
        Map<String,String> map = new HashMap();
        map.put("name",name);
        ResultBean resultBean = restTemplate.getForObject("http://order-service/testRest/getForObjectJSONParam", ResultBean.class, map);
        return resultBean;
    }

    @RequestMapping("/getForEntityJSON")
    public ResultBean getForEntityJSON(){
        ResultBean<TestRest> resultBean = new ResultBean<>();
        List<TestRest> testRestList = Lists.newArrayList(
                new TestRest(1,"神雕侠侣","杨过"),
                new TestRest(1,"神雕侠侣","小龙女")
        );
        return resultBean.returnSuccess(testRestList);
    }

    @RequestMapping("/getForEntity")
    public ResultBean getForEntity(){
        ResponseEntity<ResultBean> responseEntity = restTemplate.getForEntity("http://order-service/testRest/getForObjectJSON", ResultBean.class);
        HttpStatus statusCode = responseEntity.getStatusCode();
        boolean xxSuccessful = statusCode.is2xxSuccessful();
        ResultBean resultBean = responseEntity.getBody();
        return resultBean;
    }

    @RequestMapping(value = "/postForEntityJSON", method = RequestMethod.POST)
    public ResultBean postForEntityJSON(TestRest testRest){
        System.out.println(testRest.toString());
        ResultBean<TestRest> resultBean = new ResultBean<>();
        List<TestRest> testRestList = Lists.newArrayList(
                new TestRest(0, "保存前:"+testRest.getTitle(), "保存前:"+testRest.getName()),
                new TestRest(new Random().nextInt(100), "保存后:"+testRest.getTitle(), "保存后:"+testRest.getName())
        );
        return resultBean.returnSuccess(testRestList);
    }

    @RequestMapping(value = "/postForEntityJSON2", method = RequestMethod.POST)
    public ResultBean postForEntityJSON2(@RequestParam String title, @RequestParam String name){
        System.out.println(title + " === " + name);
        ResultBean<TestRest> resultBean = new ResultBean<>();
        List<TestRest> testRestList = Lists.newArrayList(
                new TestRest(0, "保存前:"+title, "保存前:"+name),
                new TestRest(new Random().nextInt(100), "保存后:"+title, "保存后:"+name)
        );
        return resultBean.returnSuccess(testRestList);
    }

    @RequestMapping(value = "/postForEntityBodyJSON", method = RequestMethod.POST)
    public ResultBean postForEntityBodyJSON(@RequestBody TestRest testRest){
        System.out.println(testRest.toString());
        ResultBean<TestRest> resultBean = new ResultBean<>();
        List<TestRest> testRestList = Lists.newArrayList(
                new TestRest(0, "保存前:"+testRest.getTitle(), "保存前:"+testRest.getName()),
                new TestRest(new Random().nextInt(100), "保存后:"+testRest.getTitle()+"加强版", "保存后:"+testRest.getName()+"加强版")
        );
        return resultBean.returnSuccess(testRestList);
    }

    @RequestMapping(value = "/postForEntity", method = RequestMethod.POST)
    public ResultBean postForEntity(TestRest testRest){
        System.out.println(testRest.toString());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
        map.add("name", testRest.getName());
        map.add("title", testRest.getTitle());
        HttpEntity httpEntity = new HttpEntity(map, headers);
        String testUrl = "http://order-service/testRest/postForEntityJSON";
        ResponseEntity<ResultBean> responseEntity = restTemplate.postForEntity(testUrl, map, ResultBean.class);
        return responseEntity.getBody();
    }

    @RequestMapping(value = "/postForEntityBody", method = RequestMethod.POST)
    public ResultBean postForEntityBody(@RequestBody TestRest testRest){
        System.out.println(testRest.toString());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = new HttpEntity(testRest, headers);
        String testUrl = "http://order-service/testRest/postForEntityBodyJSON";
        ResponseEntity<ResultBean> responseEntity = restTemplate.postForEntity(testUrl, httpEntity, ResultBean.class);
        return responseEntity.getBody();
    }
}
